/*
 * Copyright (C) 2011 SANSOU SYSTEMS Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package jp.co.sansou.android.atnd.util;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import jp.co.sansou.android.atnd.beans.AtndEventBean;
import jp.co.sansou.android.atnd.beans.AtndUsersBean;
import jp.co.sansou.android.atnd.beans.AtndUsersSubBean;
import jp.co.sansou.android.atnd.common.Util;
import jp.co.sansou.android.atnd.util.AtndEventSearch.AtndEventJsonKeys;
import jp.co.sansou.android.atnd.util.AtndEventSearch.AtndEventJsonSubKeys;
import jp.co.sansou.android.atnd.util.AtndUsersSearch.AtndUsersJsonKeys;
import jp.co.sansou.android.atnd.util.AtndUsersSearch.AtndUsersJsonSubKeys;
import jp.co.sansou.android.atnd.util.AtndUsersSearch.AtndUsersJsonSubSubKeys;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * ATNDのイベント情報を保持したJSONオブジェクトに関する処理を行うクラスです。
 *
 * @author y-yamaguchi
 */
public class JsonObj4List {

    /**
     * <pre>
     * ATNDイベントのイベント情報を保持したリストを返します。
     * </pre>
     *
     * @return ATNDイベント情報リスト
     * @throws JSONException
     * @throws ParseException
     */
	public static List<AtndEventBean> getEventList(JSONObject eventJson)
			throws JSONException, ParseException {
        List<AtndEventBean> result = new ArrayList<AtndEventBean>();
        JSONArray eventArray = eventJson.getJSONArray(AtndEventJsonKeys.EVENTS.toString());

        for (int i = 0; i < eventArray.length(); i++) {
            JSONObject json = eventArray.getJSONObject(i);
            AtndEventBean bean = new AtndEventBean();
            bean.setEventId(json.getInt(AtndEventJsonSubKeys.EVENT_ID.toString()));
            bean.setTitle(json.getString(AtndEventJsonSubKeys.TITLE.toString()));
            bean.setTheme(json.getString(AtndEventJsonSubKeys.THEME.toString()));
            bean.setDescription(json.getString(AtndEventJsonSubKeys.DESCRIPTION.toString()));
            bean.setEventUrl(json.getString(AtndEventJsonSubKeys.EVENT_URL.toString()));
			bean.setStartedAt(json.getString(AtndEventJsonSubKeys.STARTED_AT
					.toString()));
			bean.setEndedAt(json.getString(AtndEventJsonSubKeys.ENDED_AT
					.toString()));
            bean.setUrl(json.getString(AtndEventJsonSubKeys.URL.toString()));
			bean.setLimit(json.getInt(AtndEventJsonSubKeys.LIMIT.toString()));
            bean.setAddress(json.getString(AtndEventJsonSubKeys.ADDRESS.toString()));
            bean.setPlace(json.getString(AtndEventJsonSubKeys.PLACE.toString()));
			bean.setLat(json.getDouble(AtndEventJsonSubKeys.LAT.toString()));
			bean.setLon(json.getDouble(AtndEventJsonSubKeys.LON.toString()));
            bean.setOwnerId(json.getInt(AtndEventJsonSubKeys.OWNER_ID.toString()));
            bean.setOwnerNickname(json.getString(AtndEventJsonSubKeys.OWNER_NICKNAME.toString()));
            bean.setOwnerTwitterId(json.getString(AtndEventJsonSubKeys.OWNER_TWITTER_ID.toString()));
            bean.setAccepted(json.getInt(AtndEventJsonSubKeys.ACCEPTED.toString()));
            bean.setWaiting(json.getInt(AtndEventJsonSubKeys.WAITING.toString()));
			bean.setUpdatedAt(json.getString(AtndEventJsonSubKeys.UPDATED_AT
					.toString()));
            result.add(bean);
        }
        return result;
    }

	/**
	 * <pre>
	 * ATND参加者（ユーザ）情報を保持したリストを返します。
	 * </pre>
	 * 
	 * @return ATNDイベント情報リスト
	 * @throws JSONException
	 * @throws ParseException
	 */
	public static List<AtndUsersBean> getUsersList(JSONObject userJson)
			throws JSONException, ParseException {
		List<AtndUsersBean> result = new ArrayList<AtndUsersBean>();
		JSONArray eventArray = userJson.getJSONArray(AtndUsersJsonKeys.EVENTS
				.toString());

		for (int i = 0; i < eventArray.length(); i++) {
			JSONObject eventJson = eventArray.getJSONObject(i);
			AtndUsersBean bean = new AtndUsersBean();
			bean.setEventId(eventJson.getInt(AtndUsersJsonSubKeys.EVENT_ID
					.toString()));
			bean.setTitle(eventJson.getString(AtndUsersJsonSubKeys.TITLE
					.toString()));
			bean.setEventUrl(eventJson.getString(AtndUsersJsonSubKeys.EVENT_URL
					.toString()));
			bean.setLimit(eventJson.getString(AtndUsersJsonSubKeys.LIMIT
					.toString()));
			bean.setAccepted(eventJson.getInt(AtndUsersJsonSubKeys.ACCEPTED
					.toString()));
			bean.setWaiting(eventJson.getInt(AtndUsersJsonSubKeys.WAITING
					.toString()));
			bean.setUpdatedAt(Util.formatDateIso8601(eventJson
					.getString(AtndUsersJsonSubKeys.UPDATED_AT.toString())));

			JSONArray userArray = eventJson
					.getJSONArray(AtndUsersJsonSubKeys.USERS.toString());
			if (userArray != null && userArray.length() > 0) {
				List<AtndUsersSubBean> userList = new ArrayList<AtndUsersSubBean>();
				for (int j = 0; j < userArray.length(); j++) {
					JSONObject json = userArray.getJSONObject(j);
					AtndUsersSubBean subBean = new AtndUsersSubBean();
					subBean.setUserId(json
							.getInt(AtndUsersJsonSubSubKeys.USER_ID.toString()));
					subBean.setNickname(json
							.getString(AtndUsersJsonSubSubKeys.NICKNAME
									.toString()));
					subBean.setTwitterId(json
							.getString(AtndUsersJsonSubSubKeys.TWITTER_ID
									.toString()));
					subBean.setTwitterImg(json
							.getString(AtndUsersJsonSubSubKeys.TWITTER_IMG
									.toString()));
					subBean.setStatus(json
							.getInt(AtndUsersJsonSubSubKeys.STATUS.toString()));
					userList.add(subBean);
				}
				bean.setUsers(userList);
			}

			result.add(bean);
		}
		return result;
	}
}
